<?php
#搜索
?>
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>搜索结果</title>
    <link rel="stylesheet" href="css/index.css" />
  </head>

  <body>
    <div class="main">
      <?php
        require ("connect.php");
        $key = $_GET['key'];
        $value = $_GET['value'];
        // echo '$key='.$key;
      ?>
      <div class="display">
        <?php
            if(isset($_GET['page'])){
                $currentPage = $_GET['page'];
            }else{
                $currentPage = 1;
            }
            $query = "select * from tb_shangpin where $key like '%$value%'";
            $result = mysqli_query($link, $query) or die('查询失败'.mysqli_error());
            $total = mysqli_num_rows($result);
            $pageSize = 8;
            if($total % $pageSize == 0){
              $numOfPage = (int)($total / $pageSize);
            }else{
              $numOfPage = (int)($total / $pageSize) + 1;
            }
            $start = ($currentPage - 1) * $pageSize;
            $query2 = "select * from tb_shangpin where $key like '%$value%' limit $start, $pageSize";
            $result2 = mysqli_query($link, $query2) or die('查询失败'.mysqli_error());
            while($row = mysqli_fetch_array($result2)){ 
        ?>
        <div class="goods">
            <a href="details.php?id=<?php echo $row['id'];?>&name=<?php echo $row['name'];?>">
                <img src="<?php echo $row['photo']; ?>" alt="" width="200" height="200"/>
            </a>
            <h3 class="price">￥<?php echo $row['vipPrice']; ?></h3>
            <h4 class="name"><?php echo $row['name']; ?></h4>
        </div>
        <?php
            }
            mysqli_close($link);
        ?>
      </div>
    </div>
    <div class="clear" style="clear:both;"></div>
    <div class="page">
        <p>搜索到“<?php echo $value?>”相关记录<?php echo $total;?>条&nbsp;&nbsp;目前第<?php echo $currentPage;?>页/共<?php echo $numOfPage;?>页</p>
        <?php
            if ($numOfPage > 1 && $currentPage > 1) {
                echo "<a href='search.php?page=1&key=" . urlencode($key) . "&value=" . urlencode($value) . "'>首页</a>";
                echo "<a href='search.php?page=" . ($currentPage - 1) . "&key=" . urlencode($key) . "&value=" . urlencode($value) . "'>上一页</a>";
            }
            
            if ($numOfPage > 1 && $currentPage < $numOfPage) {
                echo "<a href='search.php?page=" . ($currentPage + 1) . "&key=" . urlencode($key) . "&value=" . urlencode($value) . "'>下一页</a>";
                echo "<a href='search.php?page=" . $numOfPage . "&key=" . urlencode($key) . "&value=" . urlencode($value) . "'>尾页</a>";
            }
        ?>
    </div>
  </body>
</html>
